﻿using BrnMall.WeiXin.Sdk.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrnMall.WeiXin.Sdk.Work
{
    public class UserHelper : WorkWeiXinSDK
    {
        /// <summary>
        /// 通讯录验证
        /// </summary>
        /// <param name="sVerifyMsgSig"></param>
        /// <param name="sVerifyTimeStamp"></param>
        /// <param name="sVerifyNonce"></param>
        /// <param name="sVerifyEchoStr"></param>
        /// <returns></returns>
        public static string Validate(string sVerifyMsgSig, string sVerifyTimeStamp, string sVerifyNonce, string sVerifyEchoStr)
        {
            string sEchoStr = "";
            WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(UserConfig.Token, UserConfig.EncodingAESKey, UserConfig.CorpID);
            int ret = wxcpt.VerifyURL(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr, ref sEchoStr);
            if (ret != 0)
            {
                return "ERR: VerifyURL fail, ret: " + ret;
            }
            //ret==0表示验证成功，sEchoStr参数表示明文，用户需要将sEchoStr作为get请求的返回参数，返回给企业号。
            return sEchoStr;
        }

        /// <summary>
        /// 获取应用套件凭证
        /// </summary>
        /// <param name="suite_ticket"></param>
        /// <returns></returns>
        public static Suite_Token Get_Suite_Token(string suite_ticket)
        {
            return Get_Suite_Token(suite_ticket, UserConfig.suite_id, UserConfig.suite_secret);
        }

        /// <summary>
        /// 获取企业access_token
        /// </summary>
        /// <param name="tokenModel"></param>
        /// <param name="permanent_code">永久授权码</param>
        /// <param name="auth_corpid">授权人的corpid,在获取永久授权码的内容中获取</param>
        /// <returns></returns>
        public static AccessToken get_corp_token(Suite_Token tokenModel, string permanent_code, string auth_corpid)
        {
            return get_corp_token(tokenModel, permanent_code, auth_corpid, UserConfig.suite_id);
        }

        /// <summary>
        /// 获取安装应用的路径
        /// </summary>
        public static string GetInstallUrl(PreAuthCode autoCode, string state, string storeId)
        {
            return GetInstallUrl(autoCode, state, storeId, UserConfig.suite_id, UserConfig.redirect_uri);
        }
    }
}
